package com.example.springboot.web;

import com.example.springboot.bean.Bbs;
import com.example.springboot.mapper.BbsMapper;
import com.example.springboot.mapper.TypeMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class BbsAction {

    @Resource
    BbsMapper bm;

    @Resource
    TypeMapper tm;

    // model 用于向页面推送数据 ==> thymeleaf
    @GetMapping("tobbs")
    public String toBbs(Bbs bbs, Model model,
                        @RequestParam(defaultValue = "1") int page,
                        @RequestParam(defaultValue = "20") int size) {
        PageHelper.startPage(page, size, true);
        model.addAttribute("blist", bm.selectByObj(bbs));
        model.addAttribute("tlist", tm.selectAll());
        return "bbs";
    }

    // ajsx  ==> model 无用  ==> 直接返回数据
    @GetMapping("queryPage")
    @ResponseBody
    public List<Bbs> queryPage(Bbs bbs,
                               @RequestParam(defaultValue = "1") int page,
                               @RequestParam(defaultValue = "20") int size) {
        PageHelper.startPage(page, size, true);
        return bm.selectByObj(bbs);
    }


}
